 /* ********************************************************************************
  *    Copyright:  (C) 2023 LN
  *    All rights reserved.
  *
  *      Filename: 	 client.c
  *    Description:  This file 
  *                 
  *      Version:  1.0.0(2023年04月05日)
  *       Author:  Li Haipeng <NULL>
  *    ChangeLog:  1, Release initial version on "2023年04月05日 23时38分38秒"
  *                    
  *
  ********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include "socket_client.h"
#include "database.h"
#include "logger.h"


void print_usage(char *progname)
{
    printf("%s usage:\n",progname);
    printf("-H(--host):sepcify host name\n");
    printf("-p(--port):sepcify server port\n");
    printf("-i(--interval):acquisition time\n");
    printf("-d(--devsn):devsn number\n");
    printf("-h(--help):print this help infomation.\n");
    return ;
}


int main(int argc,char **argv)
{
    int                      get_data_flag = 0;
    int                      interval = 3;
    int                      ch;
    int                      rv = 0;
    char                     log[16] = "temp.log";
	time_t					 cur_time;
	time_t					 now_time;
    packet_t                 pack;
    socket_t                 sock;


    struct option            opts[] = {
                                        {"Host", no_argument, NULL,'H'},
                                        {"port", no_argument, NULL,'p'},
                                        {"devsn", no_argument, NULL,'d'},
                                        {"interval", no_argument, NULL,'i'},
                                        {"help", no_argument, NULL,'h'},
                                        {NULL,0,NULL,0},
                                      };


	sock.connected = 0;
	strcpy(pack.devsn, "PRI0001");
	strcpy(sock.host, "127.0.0.1");
	sock.port = 6666;

    while((ch = getopt_long(argc , argv ,"H:p:i:d:h" , opts , NULL)) != -1)
    {
        switch(ch)
        {
            case 'd':
                strncpy(pack.devsn, optarg, sizeof(pack.devsn));
                break;
            case 'i':
                interval = atoi(optarg);
                break;
            case 'H':
                strncpy(sock.host, optarg, sizeof(pack.devsn));
                break;
            case 'p':
                sock.port = atoi(optarg);
                break;
            case 'h':
                print_usage(argv[0]);
                return 0;
        }
    }

    if(logger_init(NULL, -1) < 0)
    {
        fprintf(stderr,"initial logger system failure\n");
		return 0;
    }

   	if( !open_db(pack.devsn) )
	{
		log_error("Open data failure!");
		return 0;
	}

	cur_time = 0;

    while(1)
	{	    
		get_data_flag = 0;

		time(&now_time);
		if( (now_time - cur_time) > interval)
		{
        	if(( get_data(&pack)) < 0 )
			{
				log_error("get data failure!\n");
			}

			cur_time = now_time;

			get_data_flag = 1;
		}


		if( !socket_diag(&sock) )	
        {
            if( socket_connect(&sock) < 0)	
            {
                log_error("socket connect failure!\n");		
				socket_close(&sock);
            }
        }


		if( !sock.connected )	
		{	
			if(get_data_flag)
			{
           		rv = insert_data(&pack);
				if( !rv )
				{
					log_error("fail to insert!");
				}
			}
			continue;
		}


		if(get_data_flag)
		{
			if( !socket_data_send(&sock, &pack) )
			{
				log_error("Send data failure!\n");
				socket_close(&sock);
            	insert_data(&pack);
			}
			log_debug("Send data successfully!\n");
		}
					
		if( pop_data(&pack) )	
		{
			if( !socket_data_send(&sock, &pack) )
			{
				log_error("Send data failure!\n");
				socket_close(&sock);
			}
			else
			{
				rv = del_data(pack.devsn);
				if( !rv )
				{
					log_error("fail to delete!");
				}
			}
		}
	}
    return 0;
}

